// Package data @author <chengjiang@buffalo-robot.com>
// @date 2023/3/3
// @note
package data

import (
	"context"
	"github.com/go-kratos/kratos/v2/log"
	"gorm.io/gorm"
	"phm/api/phm/v1/bigscreen"
	"phm/internal/biz"
	"phm/models/generator/model"
	"phm/models/generator/query"
)

type raRadarRepo struct {
	Db  biz.DataRepo
	log *log.Helper
}

func NewRaRadarRepo(data biz.DataRepo, logger log.Logger) biz.RaRadarRepo {
	return &raRadarRepo{Db: data, log: log.NewHelper(logger)}
}

func (u *raRadarRepo) CreateRaRadar(ctx context.Context, uModel *model.RaRadar, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).RaRadar
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *raRadarRepo) UpdateRaRadar(ctx context.Context, uModel *model.RaRadar, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).RaRadar
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新角色错误：%v", err)
		return err
	}
	return nil
}

func (u *raRadarRepo) DeleteRaRadarByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).RaRadar
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *raRadarRepo) GetRaRadarById(ctx context.Context, id string) (*model.RaRadar, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaRadar
	user, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq("0"), opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (u *raRadarRepo) GetRaRadarByIds(ctx context.Context, ids []string) ([]*model.RaRadar, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaRadar
	users, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq("0"), opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *raRadarRepo) GetDB() *gorm.DB {
	return u.Db.GetDB()
}

func (u *raRadarRepo) CreateRaStation(ctx context.Context, uModel *model.RaStation, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).RaStation
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *raRadarRepo) UpdateRaStation(ctx context.Context, uModel *model.RaStation, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).RaStation
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(*uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新角色错误：%v", err)
		return err
	}
	return nil
}

func (u *raRadarRepo) DeleteRaStationByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).RaStation
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *raRadarRepo) GetRaStationById(ctx context.Context, id string) (*model.RaStation, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaStation
	user, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (u *raRadarRepo) GetRaStationByIds(ctx context.Context, ids []string) ([]*model.RaStation, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaStation
	users, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *raRadarRepo) CreateRaServerArea(ctx context.Context, uModel *model.RaServerArea, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).RaServerArea
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *raRadarRepo) UpdateRaServerArea(ctx context.Context, uModel *model.RaServerArea, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).RaServerArea
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(*uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新角色错误：%v", err)
		return err
	}
	return nil
}

func (u *raRadarRepo) DeleteRaServerAreaByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).RaServerArea
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *raRadarRepo) GetRaServerAreaById(ctx context.Context, id string) (*model.RaServerArea, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaServerArea
	user, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (u *raRadarRepo) GetRaServerAreaByIds(ctx context.Context, ids []string) ([]*model.RaServerArea, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaServerArea
	users, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *raRadarRepo) CreateFiRadarType(ctx context.Context, uModel *model.FiRadarType, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).FiRadarType
	err := opt.WithContext(ctx).Create(uModel)
	if err != nil {
		return err
	}
	return nil
}

func (u *raRadarRepo) UpdateFiRadarType(ctx context.Context, uModel *model.FiRadarType, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	opt := query.Use(s).FiRadarType
	info, err := opt.WithContext(ctx).Where(opt.ID.Eq(uModel.ID)).Updates(uModel)
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("更新角色错误：%v", err)
		return err
	}
	return nil
}

func (u *raRadarRepo) DeleteFiRadarTypeByIds(ctx context.Context, ids []string, db *gorm.DB) error {
	var s = db
	if db == nil {
		s = u.Db.GetDB()
	}
	role := query.Use(s).FiRadarType
	info, err := role.WithContext(ctx).Where(role.ID.In(ids...)).Delete()
	u.log.Info(info)
	if err != nil {
		u.log.Errorf("删除错误：%v", ids)
		return err
	}
	return nil
}

func (u *raRadarRepo) GetFiRadarTypeById(ctx context.Context, id string) (*model.FiRadarType, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).FiRadarType
	user, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.Eq(id)).First()
	if err != nil {
		return nil, err
	}
	return user, nil
}

func (u *raRadarRepo) GetFiRadarTypeByIds(ctx context.Context, ids []string) ([]*model.FiRadarType, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).FiRadarType
	users, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0), opt.ID.In(ids...)).Find()
	if err != nil {
		return nil, err
	}
	return users, nil
}

func (u *raRadarRepo) GetRaRadarList(ctx context.Context, queryParam string, pageNum, pageSize int64) ([]*model.RaRadar, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaRadar
	if pageNum == 0 && pageSize == 0 {
		radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq("0")).Find()
		if err != nil {
			return nil, err
		}
		return radars, nil
	}
	radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq("0")).Limit(int(pageSize)).Offset(int((pageNum - 1) * pageSize)).Find()
	if err != nil {
		return nil, err
	}
	return radars, nil
}

func (u *raRadarRepo) GetRaStationList(ctx context.Context, queryParam string, pageNum, pageSize int64) ([]*model.RaStation, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaStation
	if pageNum == 0 && pageSize == 0 {
		radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Find()
		if err != nil {
			return nil, err
		}
		return radars, nil
	}
	radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Limit(int(pageSize)).Offset(int((pageNum - 1) * pageSize)).Find()
	if err != nil {
		return nil, err
	}
	return radars, nil
}

func (u *raRadarRepo) GetRaServerAreaList(ctx context.Context, queryParam string, pageNum, pageSize int64) ([]*model.RaServerArea, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).RaServerArea
	if pageNum == 0 && pageSize == 0 {
		radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Find()
		if err != nil {
			return nil, err
		}
		return radars, nil
	}
	radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Limit(int(pageSize)).Offset(int((pageNum - 1) * pageSize)).Find()
	if err != nil {
		return nil, err
	}
	return radars, nil
}

func (u *raRadarRepo) GetFiRadarTypeList(ctx context.Context, queryParam string, pageNum, pageSize int64) ([]*model.FiRadarType, error) {
	s := u.Db.GetDB()
	opt := query.Use(s).FiRadarType
	if pageNum == 0 && pageSize == 0 {
		radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Find()
		if err != nil {
			return nil, err
		}
		return radars, nil
	}
	radars, err := opt.WithContext(ctx).Where(opt.IsSoftDelete.Eq(0)).Limit(int(pageSize)).Offset(int((pageNum - 1) * pageSize)).Find()
	if err != nil {
		return nil, err
	}
	return radars, nil
}

// GetRadarSubSystem 获取子系统
func (u *raRadarRepo) GetRadarSubSystem(ctx context.Context, id string) ([]*bigscreen.SubSystemReply_SubSystemItem, error) {
	res := []*bigscreen.SubSystemReply_SubSystemItem{}
	s := u.Db.GetDB()
	frtfs := query.Use(s).FiRealTimeFileStructure
	dli := query.Use(s).DisplayLocationInfo
	rs := query.Use(s).RaSystem
	rr := query.Use(s).RaRadar
	frt := query.Use(s).FiRadarType
	fft := query.Use(s).FiFileType
	rt := query.Use(s).RaTree
	// 获取子系统
	err := frtfs.WithContext(ctx).Select(rs.ID.Distinct(), rs.Name).
		LeftJoin(dli, dli.ID.EqCol(frtfs.DisplayLocationID)).
		LeftJoin(rs, rs.ID.EqCol(frtfs.SystemID)).
		Where(
			frtfs.WithContext(ctx).Columns(frtfs.FileTypeID).
				Eq(rr.WithContext(ctx).Select(fft.ID).
					LeftJoin(frt, rr.TypeID.EqCol(frt.ID)).
					LeftJoin(fft, fft.RadarTypeID.EqCol(frt.ID)).
					Where(rr.IsSoftDelete.Eq("0"), frt.IsSoftDelete.Eq(0), rr.ID.Eq(id)),
				),
			rs.IsSoftDelete.Eq(0),
			frtfs.IsSoftDelete.Eq(0),
			rs.WithContext(ctx).Columns(rs.ID).In(
				rt.WithContext(ctx).Distinct(rt.SystemID).
					Where(rt.RadarID.Eq(id),
						rt.IsSoftDelete.Eq(0),
						rt.ExtensionID.IsNull(),
						rt.SystemID.IsNotNull()),
			),
		).Scan(&res)
	if err != nil {
		return nil, err
	}
	return res, nil
}
